Order Management

Creating Orders

create_order(self, account_id, **params)


In [1]:
from datetime import datetime, timedelta
import pandas as pd
import oandapy
import configparser

config = configparser.ConfigParser()
config.read('../config/config_v1.ini')
account_id = config['oanda']['account_id']
api_key = config['oanda']['api_key']

oanda = oandapy.API(environment="practice", 
                    access_token=api_key)

In [2]:
trade_expire = datetime.now() + timedelta(days=1)
trade_expire = trade_expire.isoformat("T") + "Z"
trade_expire


Out[2]:
'2017-01-28T21:54:17.120062Z'

For a detailed explanation of the above, please refer to Rates Information.


In [3]:
response = oanda.create_order(account_id,
                              instrument = "AUD_USD",
                              units=1000,
                              side="buy",
                              type="limit",
                              price=0.7420,
                              expiry=trade_expire)
print(response)


{'orderOpened': {'takeProfit': 0, 'side': 'buy', 'upperBound': 0, 'expiry': '2017-01-28T21:54:17.000000Z', 'trailingStop': 0, 'stopLoss': 0, 'id': 10618881403, 'lowerBound': 0, 'units': 1000}, 'price': 0.742, 'instrument': 'AUD_USD', 'time': '2017-01-27T13:54:18.000000Z'}

In [4]:
pd.Series(response["orderOpened"])


Out[4]:
expiry          2017-01-28T21:54:17.000000Z
id                              10618881403
lowerBound                                0
side                                    buy
stopLoss                                  0
takeProfit                                0
trailingStop                              0
units                                  1000
upperBound                                0
dtype: object

In [5]:
order_id = response["orderOpened"]['id']

Getting Open Orders

get_orders(self, account_id, **params)


In [6]:
response = oanda.get_orders(account_id)
print(response)


{'orders': [{'takeProfit': 0, 'type': 'limit', 'side': 'buy', 'trailingStop': 0, 'price': 0.742, 'upperBound': 0, 'instrument': 'AUD_USD', 'id': 10618881403, 'units': 1000, 'expiry': '2017-01-28T21:54:17.000000Z', 'stopLoss': 0, 'lowerBound': 0, 'time': '2017-01-27T13:54:18.000000Z'}]}

In [7]:
pd.DataFrame(response['orders'])


Out[7]:
expiry id instrument lowerBound price side stopLoss takeProfit time trailingStop type units upperBound
0 2017-01-28T21:54:17.000000Z 10618881403 AUD_USD 0 0.742 buy 0 0 2017-01-27T13:54:18.000000Z 0 limit 1000 0

Getting Specific Order Information

get_order(self, account_id, order_id, **params)


In [8]:
response = oanda.get_orders(account_id)
id = response['orders'][0]['id']

In [9]:
oanda.get_order(account_id, order_id=id)


Out[9]:
{'expiry': '2017-01-28T21:54:17.000000Z',
 'id': 10618881403,
 'instrument': 'AUD_USD',
 'lowerBound': 0,
 'price': 0.742,
 'side': 'buy',
 'stopLoss': 0,
 'takeProfit': 0,
 'time': '2017-01-27T13:54:18.000000Z',
 'trailingStop': 0,
 'type': 'limit',
 'units': 1000,
 'upperBound': 0}

Modify Order

modify_order(self, account_id, order_id, **params)


In [10]:
response = oanda.get_orders(account_id)
id = response['orders'][0]['id']

In [11]:
oanda.modify_order(account_id, order_id=id, price=0.7040)


Out[11]:
{'expiry': '2017-01-28T21:54:17.000000Z',
 'id': 10618881403,
 'instrument': 'AUD_USD',
 'lowerBound': 0,
 'price': 0.704,
 'side': 'buy',
 'stopLoss': 0,
 'takeProfit': 0,
 'time': '2017-01-27T13:54:20.000000Z',
 'trailingStop': 0,
 'type': 'limit',
 'units': 1000,
 'upperBound': 0}

Close Order

close_order(self, account_id, order_id, **params)


In [12]:
response = oanda.get_orders(account_id)
id = response['orders'][0]['id']

In [13]:
oanda.close_order(account_id, order_id=id)


Out[13]:
{'id': 10618881403,
 'instrument': 'AUD_USD',
 'price': 0.704,
 'side': 'buy',
 'time': '2017-01-27T13:54:20.000000Z',
 'type': 'BuyLimit',
 'units': 1000}

Now when we check the orders. The above order has been closed and removed without being filled. There is only one outstanding order now.


In [14]:
oanda.get_orders(account_id)


Out[14]:
{'orders': []}